/**
  ******************************************************************************
  * @file    Project/main.c 
  * @author  MCD Application Team
  * @brief   Main program body
  ******************************************************************************
  *
  ******************************************************************************
  */ 


/* Includes ------------------------------------------------------------------*/

#include "osObjects.h"                      // RTOS object definitions
#include "system_MKE06Z4.h"  
#include "systimer.h"
#include "common.h"

/* Private defines -----------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
void PortInit(void);

/*****************************************************************************//*!
* @brief   	main .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
int main()
{
	PortInit();
	//OscFllInit();
    IrcFllInit();
    //OscInit();
    
    SystemCoreClockUpdate();

    osKernelInitialize ();                    // initialize CMSIS-RTOS

    // initialize peripherals here
    // create 'thread' functions that start executing,
    Init_Thread ();

    osKernelStart ();                         // start thread execution 
    
    osThreadYield ();                         // suspend thread
}

/*****************************************************************************//*!
* @brief   	port init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void PortInit()
{
	
	SIM->SOPT0  &= ~SIM_SOPT0_NMIE_MASK;                           //disable NMI
	
	//Port Filter Config
	PORT->IOFLT0 =  (5UL << PORT_IOFLT0_FLTDIV3_SHIFT) |           //32ms
					(1UL << PORT_IOFLT0_FLTDIV2_SHIFT) |           //BusClk / 64
					(3UL << PORT_IOFLT0_FLTDIV1_SHIFT) |           //BusClk / 16
					(3UL << PORT_IOFLT0_FLTNMI_SHIFT)  |           //DIV3 for NMI
					(3UL << PORT_IOFLT0_FLTKBI0_SHIFT) |           //DIV3 for KBI0
					(3UL << PORT_IOFLT0_FLTKBI1_SHIFT) |           //DIV3 for KBI1
					(3UL << PORT_IOFLT0_FLTRST_SHIFT)  |           //DIV3 for RST
					(0UL << PORT_IOFLT0_FLTH_SHIFT)    |           // for PTH
					(0UL << PORT_IOFLT0_FLTG_SHIFT)    |           // for PTG
					(0UL << PORT_IOFLT0_FLTF_SHIFT)    |           // for PTF
					(0UL << PORT_IOFLT0_FLTE_SHIFT)    |           // for PTE
					(0UL << PORT_IOFLT0_FLTD_SHIFT)    |           // for PTD
					(0UL << PORT_IOFLT0_FLTC_SHIFT)    |           // for PTC
					(0UL << PORT_IOFLT0_FLTB_SHIFT)    |           // for PTB
					(0UL << PORT_IOFLT0_FLTA_SHIFT);               // for PTA
					                  
	PORT->IOFLT1 =  (0UL << PORT_IOFLT1_FLTI2C1_SHIFT) |           // for I2C1
					(0UL << PORT_IOFLT1_FLTI2C0_SHIFT) |           // for I2C0
					(0UL << PORT_IOFLT1_FLTPWT_SHIFT)  |           // for PWT
					(0UL << PORT_IOFLT1_FLTFTM1_SHIFT) |           // for FTM1
					(0UL << PORT_IOFLT1_FLTFTM0_SHIFT) |           // for FTM0
					(1UL << PORT_IOFLT1_FLTIRQ_SHIFT)  |           // for IRQ
					(0UL << PORT_IOFLT1_FLTI_SHIFT);               // for PTI
	
	
	PORT->PUE0   = 0x0000000C;         //Pullup Enable : A B C D
	PORT->PUE1   = 0x00FF0010;         //Pullup Enable : E F G H
	PORT->PUE2   = 0x00000000;         //Pullup Enable : I
	
	PORT->HDRVE  = 0;                  //High Drive Enable
	
	FGPIOA->PDDR = 0x030000C0;         // Port Data Direction : A B C D 
	FGPIOB->PDDR = 0x00FF0018;         // Port Data Direction : E F G H
	FGPIOC->PDDR = 0x00000000;         // Port Data Direction : I
	
	FGPIOA->PIDR = 0xDFFFFFFF;         // Port Input Disable  : A B C D
	FGPIOB->PIDR = 0xFFFFFFFF;         // Port Input Disable  : E F G H
	FGPIOC->PIDR = 0xFFFFFFFF;         // Port Input Disable  : I
	
	FGPIOB->PTOR = 0x00FF0018;         // Port Data Out: E F G H 

}

/*****************************************************************************//*!
* @brief   	system tick hook.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void SysTickHook()
{
	SysTickCnt++;
}
